package admin

import (
	"fmt"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"github.com/gin-gonic/gin"
	"strconv"
)

// QueryAdminMenu 菜单授权列表
// @Summary 菜单授权列表
// @Description 菜单授权列表
// @Tags API.admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id path string true "hashId"
// @Success 200 {object} response.TResponse{data=vo.AdminMenuListVO}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin/menu/{id} [get]
// @Security LoginToken
func (ac *adminController) QueryAdminMenu(c *gin.Context) {
	var id = c.Param("id")
	if id == "" {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": "id 必传"})
		return
	}
	idInt, _ := strconv.Atoi(id)
	adminMenuList, err := ac.adminService.QueryAdminMenu(c, int32(idInt))

	if err == nil {
		response.Success(c, "获取菜单成功", gin.H{
			"adminMenuList": adminMenuList,
		})
	}
}

// EditAdminMenu 提交菜单授权
// @Summary 提交菜单授权
// @Description 提交菜单授权
// @Tags API.admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id formData string true "管理员id"
// @Param actions formData string true "功能权限ID,多个用,分割"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin/menu [post]
// @Security LoginToken
func (ac *adminController) EditAdminMenu(c *gin.Context) {
	var adminMenuReq QO.EditAdminMenuReq
	if err := c.ShouldBind(&adminMenuReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	fmt.Println("adminMenuReq", adminMenuReq)
	err := ac.adminService.EditAdminMenu(c, adminMenuReq)
	if err == nil {
		response.Success(c, "编辑菜单成功", gin.H{})
	}
}
